<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="商户号" prop="merchantId">
				<el-input v-model="queryParams.merchantId" placeholder="请输入商户号" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="商户名称" prop="merchantName">
				<el-input v-model="queryParams.merchantName" placeholder="请输入商户名称" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="奖品名称" prop="prizeName">
				<el-input v-model="queryParams.prizeName" placeholder="请输入奖品名称" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="奖品数量" prop="prizeAmount">
				<el-input v-model="queryParams.prizeAmount" placeholder="请输入奖品数量" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="取奖地址" prop="prizeAddress">
				<el-input v-model="queryParams.prizeAddress" placeholder="请输入取奖地址" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
				<el-col :span="1.5">
					<el-button type="info" plain icon="el-icon-check" size="mini" @click="usedprize">已选择的奖品</el-button>
					<el-button type="primary" plain icon="el-icon-check" size="mini" @click="unusedprize">未选择的奖品</el-button>
				</el-col>	
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>
		<el-table v-loading="loading" :data="prizeinfoList">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="奖品号" align="center" prop="prizeId" />
			<el-table-column label="商户号" align="center" prop="merchantId" />
			<el-table-column label="商户名称" align="center" prop="merchantName" />
			<el-table-column label="奖品名称" align="center" prop="prizeName" />
			<el-table-column label="奖品数量" align="center" prop="prizeAmount" />
			<!-- <el-table-column label="奖品图片" align="center" prop="prizePic" /> -->
			<el-table-column label="取奖地址" align="center" prop="prizeAddress" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="prizeuse == 0">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:prizeinfo:edit']">确认选择</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或修改奖品抽奖对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="考试号" prop="safetyExaminationId">
					<el-input v-model="form.safetyExaminationId" :disabled="true" placeholder="请输入考试号" />
				</el-form-item>
				<el-form-item label="奖品号" prop="prizeId">
					<el-input v-model="form.prizeId" :disabled="true" placeholder="请输入奖品号" />
				</el-form-item>
				<el-form-item label="奖品名称" prop="prizeName">
					<el-input v-model="form.prizeName" :disabled="true" placeholder="请输入奖品名称" />
				</el-form-item>
				<el-form-item label="奖品数量" prop="prizeAmount">
					<el-input v-model="form.prizeAmount" placeholder="请输入奖品数量"  type="number" :min="0" :max="prizeAmoutmax" />
					<spqn style="color: #9c9c9c;">ps:剩余奖品数量为{{ form.prizeSurplusAmount }}</spqn>
				</el-form-item>
				<el-form-item label="奖品概率" prop="prizeProbability">
					<el-input v-model="form.prizeProbability" placeholder="请输入奖品概率" type="number" :min="0" :max="form.prizeSurplusProbability" :step="0.1" />
					<spqn style="color: #9c9c9c;">ps:剩余最大概率为{{ form.prizeSurplusProbability }}</spqn>
				</el-form-item>
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="form.setId" :disabled="true" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确定选择</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listPrizeinfo,
		getPrizeinfo,
		delPrizeinfo,
		addPrizeinfo,
		updatePrizeinfo,
		exportPrizeinfo,
		addPrizeluck,
		Prizeinfo
	} from "@/api/system/prizeinfo";
	import {
		getToken
	} from "@/utils/auth";
	export default {
		name: "Prizeinfo",
		components: {},
		data() {
			return {
				fileList: [],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				//是否已选择
				prizeuse:0,
				//奖品最大数
				prizeAmoutmax: 0,
				// 奖品表格数据
				prizeinfoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					merchantId: null,
					merchantName: null,
					prizeName: null,
					prizeAmount: null,
					prizePic: null,
					prizeAddress: null
				},
				//确认选择查看详情
				prizeParams:{
					ExaminationPaperId: null,
					prizeId: null,
					prizeSurplusProbability: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					merchantId: [{
						required: true,
						message: "商户号不能为空",
						trigger: "blur"
					}],
					merchantName: [{
						required: true,
						message: "商户名称不能为空",
						trigger: "blur"
					}],
					prizeName: [{
						required: true,
						message: "奖品名称不能为空",
						trigger: "blur"
					}],
					prizeAmount: [{
						required: true,
						message: "奖品数量不能为空",
						trigger: "blur"
					}],
					prizeAddress: [{
						required: true,
						message: "取奖地址不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询奖品列表 */
			getList() {
				this.loading = true;
				this.queryParams.safetyExaminationId=this.$route.query.safetyExaminationPaperId;
				this.queryParams.memo="2";
				listPrizeinfo(this.queryParams).then(response => {
					this.prizeinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					prizeId: null,
					merchantId: null,
					merchantName: null,
					prizeName: null,
					prizeAmount: null,
					prizePic: null,
					prizeAddress: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const prizeId = row.prizeId || this.ids
				this.prizeParams.prizeId=prizeId;
				this.prizeParams.merchantId=this.$route.query.safetyExaminationPaperId;
				this.prizeParams.prizeSurplusProbability=1;
				Prizeinfo(this.prizeParams).then(response => {
					this.form = response.data;
					this.form.safetyExaminationId = this.$route.query.safetyExaminationPaperId;
					this.form.setId = this.$store.getters.userId;
					this.prizeAmoutmax = this.form.prizeAmount;
					this.open = true;
					this.title = "编辑选择的奖品信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.prizeId != null) {
							console.log(this.form)
							addPrizeluck(this.form).then(response => {
								this.msgSuccess("选择成功");
								this.open = false;
								this.getList();
								this.$router.push({
									path: '/safetyexam/safetyexam',
									// /prizesetting
									// query: {
									// 	safetyExaminationId: this.$route.query.safetyExaminationPaperId
									// }
								})
							});
						}
					}
				});
			},
			//已选择的奖品
			usedprize(){
				this.queryParams.memo="1";
				this.loading = true;
				this.queryParams.safetyExaminationId=this.$route.query.safetyExaminationPaperId;
				listPrizeinfo(this.queryParams).then(response => {
					this.prizeinfoList = response.rows;
					this.total = response.total;
					this.prizeuse = 1;
					this.loading = false;
				});
			},
			//未选择的奖品
			unusedprize(){
				this.queryParams.memo="2";
				this.loading = true;
				this.queryParams.safetyExaminationId=this.$route.query.safetyExaminationPaperId;
				listPrizeinfo(this.queryParams).then(response => {
					this.prizeinfoList = response.rows;
					this.total = response.total;
					this.prizeuse = 0;
					this.loading = false;
				});
			}
		}
	};
</script>
